package cn.org.xiaosheng.okhttp.configuration;

import cn.org.xiaosheng.okhttp.interceptor.EncryptInterceptor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.util.concurrent.TimeUnit;

/**
 * @author XiaoSheng
 * @date 2023-11-20
 * @dec OkHttp依赖注入配置类
 */
@Configuration
@Slf4j
public class OkHttpConfig {

    /**
     * 直接通过new的方式实例化：
     * @return
     */
    @ConditionalOnMissingBean(OkHttpClient.class)
    @Bean
    public OkHttpClient okHttpClient() {
        return new OkHttpClient();
    }

    /**
     * 通过OkHttpClient.Builder构造实例, 此种方式支持自定义部分行为
     * @return
     */
    @Bean("buildOkHttpClient")
    public OkHttpClient buildOkHttpClient() {
        log.info("被实例化了!");
        return new OkHttpClient.Builder()
                .addInterceptor(new EncryptInterceptor())
                .build();

    }

}
